import React, { Component } from "react";
// todo Prompt用来做路由保护的，when和message属性
import { Link, Route, Switch, Redirect, Prompt } from "react-router-dom";

class Home extends Component {
  render() {
    return <div>这是首页页面</div>;
  }
}

class About extends Component {
  state = {
    value: "",
  };
  handleChange = (e) => {
    this.setState({
      value: e.target.value,
    });
  };
  render() {
    return (
      <>
        <div>这是关于页面</div>
        <input
          type="text"
          value={this.state.value}
          onChange={this.handleChange}
        />

        <Prompt
          when={!!this.state.value}
          message={(location) =>
            `Are you sure you want to go to ${location.pathname}`
          }
        ></Prompt>
      </>
    );
  }
}

class Mine extends Component {
  render() {
    return <div>这是我的页面</div>;
  }
}

class App extends Component {
  render() {
    return (
      <>
        <h2>react-router-受保护路由</h2>
        <ul>
          <li>
            <Link to="/home">home</Link>
          </li>
          <li>
            <Link to="/about">about</Link>
          </li>
          <li>
            <Link to="/mine">mine</Link>
          </li>
        </ul>

        <hr />

        <Switch>
          <Redirect from="/" to="/home" exact></Redirect>
          <Route path="/home" component={Home}></Route>
          <Route path="/about" component={About}></Route>
          <Route path="/mine" component={Mine}></Route>
        </Switch>
      </>
    );
  }
}

export default App;
